/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.company;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jobportal.Dbconn;

/**
 *
 * @author project
 */
public class CompanyRegister extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        Connection con;
        Statement st;
        ResultSet rs;
        String error = "";
        boolean flag = true;
        String dest = "employer.jsp", desterr = "employer_registration.jsp";
        HttpSession session=request.getSession();
        if(session.getAttribute("role")!=null&&session.getAttribute("role").toString().equalsIgnoreCase("admin")){
            dest="admin/manage_emp.jsp";
            desterr="admin/employer_registration.jsp";
        }
        String compid, password, email, personname, designation, contact, fax, mobile, compname, address, state, city, type, cattype, sector, profile, url, sql;
        PrintWriter out = response.getWriter();
        try {
            con = Dbconn.getConn();
            st = con.createStatement();
            compid = request.getParameter("compid");
            password = request.getParameter("password");
            email = request.getParameter("email");
            personname = request.getParameter("personname");
            designation = request.getParameter("designation");
            contact = request.getParameter("contact");
            fax = request.getParameter("fax");
            mobile = request.getParameter("mobile");
            compname = request.getParameter("compname");
            address = request.getParameter("address");
            state = request.getParameter("state");
            city = request.getParameter("city");
            type = request.getParameter("type");
            cattype = request.getParameter("cattype");
            sector = request.getParameter("sector");
            profile = request.getParameter("profile");
            url = request.getParameter("url");
            rs = st.executeQuery("select * from company where compid='" + compid + "'");
            if (rs.next()) {
                response.sendRedirect("/employer_registration.jsp?status=Id already exist");
            } else {
                sql = "insert into company(compid,password,email,personname,designation,contact,fax,mobile,compname,address,state,city,type,cattype,sector,profile,url) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setString(1, compid);
                ps.setString(2, password);
                ps.setString(3, email);
                ps.setString(4, personname);
                ps.setString(5, designation);
                ps.setString(6, contact);
                ps.setString(7, fax);
                ps.setString(8, mobile);
                ps.setString(9, compname);
                ps.setString(10, address);
                ps.setString(11, state);
                ps.setString(12, city);
                ps.setString(13, type);
                ps.setString(14, cattype);
                ps.setString(15, sector);
                ps.setString(16, profile);
                ps.setString(17, url);
                if (ps.executeUpdate() > 0) {
                   
                    response.sendRedirect(dest);
                } else {
                    
                    response.sendRedirect(desterr+"?status=<font color='red' >Error occured! please try later </font>");
                }

            }
        } catch (Exception e) {

            error += " Exception=" + e;
            response.sendRedirect(desterr+"?status=<font color='red' >Eroor! "+e+" </font>");

        }

    }
}
